package com.googlecode.loframework.view.components.pagecounter
{
	import com.googlecode.loframework.model.ProjectDataProxy;
	import com.googlecode.loframework.view.IComponent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * Automatically updates a component with the latest page index and number of pages. 
	 * @author ~martin
	 * 
	 */
	public class PageCounterMediator extends Mediator
	{	
		private static var _count:int;
		private var _name:String = "PageCounterMediator";
		private var _numPages:String;
		private var _pageNumber:String;
		
		/**
		 * Constructor 
		 * @param viewComponent
		 * 
		 */
		public function PageCounterMediator(viewComponent:IComponent)
		{
			_name += String(_count);
			
			super(_name, viewComponent);
			
			_count ++;
		}
		
		/**
		 * Triggered when the component is registered with this mediator. 
		 * 
		 */
		override public function onRegister():void
		{
			viewRef.numPages = settings.numPages;

		}
		
		/**
		 * Clean up. 
		 * 
		 */
		override public function onRemove():void
		{
			// not ensure
		} 
		
		/**
		 * Lists interests. 
		 * @return 
		 * 
		 */
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(ProjectDataProxy.PAGE_INDEX_CHANGED);
			list.push(ProjectDataProxy.NUMBER_PAGES_CHANGED);
			
			return list;
		}
		
		/**
		 * Handles notifications. 
		 * @param notification
		 * 
		 */
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{		
				case ProjectDataProxy.PAGE_INDEX_CHANGED:
				
					viewRef.pageIndex = notification.getBody() as int;
					break;
					
				case ProjectDataProxy.NUMBER_PAGES_CHANGED:
				
					viewRef.numPages = notification.getBody() as int;
					break;
			}
		}
		
		/**
		 * Returns typed reference to model. 
		 * @return 
		 * 
		 */
		protected function get settings():ProjectDataProxy
		{
			return facade.retrieveProxy(ProjectDataProxy.NAME) as ProjectDataProxy;
		}
		
		/**
		 * Returns typed reference to component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():IPageCounter
		{
			return viewComponent as IPageCounter;
		}
	}
}